<template>
    <div class="page">
        <div class="logo">
            <img class="logo-img" src="../assets/userLogo-2.png" alt="" />
            <div class="right-ic" @click="changeLanguage">
                <img src="../assets/100-1.png" alt="" />
                <span>{{ $i18n.locale == "zh" ? "EN" : "中文" }}</span>
            </div>
        </div>
        <div class="bg">
            <img src="../assets/bg_03.png" alt="" />

            <div class="user">
                <div class="user-left">
                    <div class="user-name">
                        {{ $i18n.locale == "zh" ? userinfo.english_name : userinfo.name }}
                        <span class="tag" v-if="type !== 4">{{ userinfo.position }}</span>
                    </div>
                    <div class="user-text" v-if="type !== 4">
                        {{ $i18n.locale == "zh" ? userinfo.title : userinfo.title_en }}
                    </div>
                </div>
                <div class="user-img">
                    <img :src="userinfo.avatar" alt="" />
                </div>
            </div>
        </div>
        <div class="content">
            <div class="search-wrap">
                <!-- <div class="tab-text">
					{{$t('language.text11')}}
					<img class="ic" src="../assets/120.png" alt="" />
				</div> -->
                <div class="search">
                    <input type="text" v-model="search" :placeholder="$t('language.search')" />
                    <img class="ic" src="../assets/101.png" alt="" />
                </div>
            </div>
            <van-list v-model="loading" :finished="finished" :finished-text="$t('language.noMore')" @load="onLoad">
                <div class="card" @click="goPage('mySellDetails', item.id)" v-for="item in list" :key="item.id">
                    <div class="title" v-if="item.title">{{ item.title || "" }}</div>
                    <div class="title small">{{ item.title_alias || "" }}</div>
                    <div class="tag-wrap">
                        <!-- is_verify 0 待审核 1 成功 2 失败 -->
                        <div v-if="item.is_verify == 0" class="tag await">{{ $t("language.text12") }}</div>
                        <div v-if="item.is_verify == 1" class="tag success">{{ $t("language.text13") }}</div>
                        <div v-if="item.is_verify == 2" class="tag not">{{ $t("language.text14") }}</div>
                        <!-- <div class="tag">长期客户</div> -->
                    </div>
                    <div class="info">
                        {{
                            $i18n.locale == "zh"
                                ? `${computeDate(item.created_at) + $t("language.text15")}`
                                : `${$t("language.text15") + computeDate(item.created_at)}`
                        }}: <span class="num">{{ item.room_num || item.be_room_num }}</span>
                    </div>
                    <div class="info" style="margin-top: 4px">
                        {{ $t("language.text32") }}: <span class="num">{{ item.price_code }}</span>
                    </div>

                    <!-- is_share 这个是是否开启分享 0 不开 1开 -->
                    <div class="btn" v-if="item.is_share == 1" @click.stop="tapShare(item)">
                        {{ $t("language.text16") }}
                    </div>
                    <div class="btn not" v-if="item.is_share == 0" @click.stop="">{{ $t("language.text16") }}</div>
                </div>
            </van-list>
        </div>

        <div class="add-btn">
            <div class="btn" @click="$router.back()">{{ $t("language.text25") }}</div>
        </div>

        <van-popup v-model="showShare" class="van-popup-share" position="bottom">
            <div class="popup-share">
                <div class="share-list">
                    <div class="share-item" @click="shareLnr">
                        <img src="../assets/126.png" alt="" />
                        <span>{{ $t("language.text87") }}</span>
                    </div>
                    <div class="share-item" @click="tapShowQrcode">
                        <img src="../assets/127.png" alt="" />
                        <span>{{ $t("language.text88") }}</span>
                    </div>
                    <!-- <div class="share-item" @click="shareActivity" v-if="type == 4 && !isShowActivity ? false : true"> -->
                    <!-- <div class="share-item" @click="shareActivity" v-if="type != 4">
                        <img src="../assets/128.png" alt="" />
                        <span>{{ $t("language.text89") }}</span>
                    </div> -->
                </div>
                <div class="colse-share" @click="showShare = false">{{ $t("language.text53") }}</div>
            </div>
        </van-popup>

        <van-popup v-model="showQrcode" class="van-popup-qrcode" position="center">
            <div class="qrcode-wrap">
                <div class="qrcode" v-if="qrcode != ''">
                    <img
                        class="qrcode-img"
                        :src="qrcode"
                        alt=""
                        @mousedown="startPress"
                        @mouseup="endPress"
                        @mouseleave="endPress"
                    />
                </div>
                <van-icon name="close" @click="showQrcode = false" />
            </div>
            <div class="qrcode-cue">
                <img src="../assets/131.png" alt="" />
                <div class="cue-text">
                    <img src="../assets/132.png" alt="" />
                    <span>{{ $t("language.text86") }}</span>
                </div>
            </div>
        </van-popup>
    </div>
</template>
<script>
import {
    getMyUserLists,
    // getWxConfig,
    getWxAgentConfig,
    share,
    beUserLnrLists,
    aio_qrcode,
    get_front_config,
} from "@/api/user";

import { Toast } from "vant";

let timer = null;
export default {
    name: "MySell",
    data: function () {
        return {
            locale: "zh",
            search: "",
            userinfo: {
                hotel: {},
            },
            type: 2,
            id: "",
            list: [],
            loading: false,
            finished: false,
            page: 1,
            showShare: false,
            shareItem: {},
            showQrcode: false,
            qrcode: "",
            pressTimer: null,
            isShowActivity: false,
        };
    },
    watch: {
        search: function () {
            const t = this;
            if (timer != null) clearTimeout(timer);

            timer = setTimeout(() => {
                timer = null;
                clearTimeout(timer);
                t.page = 1;
                t.list = [];
                t.getLnrData();
            }, 500);
        },
    },
    created() {
        // this.wxConfig();
        const { query } = this.$route;
        this.id = query.id;
        const userinfo = localStorage.getItem("userinfo");
        const { type } = JSON.parse(userinfo);
        this.type = type;
        this.userinfo = query;
        this.getFrontConfig();
    },
    mounted() {},
    methods: {
        getFrontConfig() {
            get_front_config({
                config_id: 1,
            })
                .then((res) => {
                    const { is_open } = res.data;
                    if (is_open == 1) {
                        this.isShowActivity = true;
                    }
                })
                .catch(() => {});
        },
        computeDate(str) {
            let date = new Date(str);
            date.setMonth(date.getMonth() - 1);
            return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}`;
        },
        wxConfig() {
            getWxAgentConfig({
                url: location.href.split("#")[0],
                code: "launchMiniprogram,checkJsApi",
            })
                .then((res) => {
                    const data = JSON.parse(res.data);
                    wx.agentConfig({
                        corpid: "wwdc0bc731d475dfb8", // 必填，企业微信的corpid，必须与当前登录的企业一致
                        agentid: "1000156", // 必填，企业微信的应用id （e.g. 1000247）
                        timestamp: data.timestamp, // 必填，生成签名的时间戳
                        nonceStr: data.nonceStr, // 必填，生成签名的随机串
                        signature: data.signature, // 必填，签名，见附录-JS-SDK使用权限签名算法
                        jsApiList: ["launchMiniprogram", "checkJsApi", "shareWechatMessage"], //必填，传入需要使用的接口名称
                        success: function () {
                            // console.log(res, 'agentConfig---success')
                        },
                        fail: function (err) {
                            if (err.errMsg.indexOf("function not exist") > -1) {
                                console.log(err.errMsg, "fail");
                            }
                        },
                    });
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        tapShare(item) {
            this.showShare = true;
            this.shareItem = item;
        },
        tapShowQrcode() {
            this.getQrcode();
        },

        getQrcode() {
            if (this.showShare == false) return;
            this.showShare = false;

            Toast.loading({
                duration: 0,
                forbidClick: true,
            });

            const { user, agreement_price_code } = this.shareItem;

            aio_qrcode({
                merlinId: user?.merlinid,
                hotelCode: user?.hotel_code,
                corporateId: agreement_price_code,
                page: this.type == 4 ? "pages/home/home" : "bookingFlow/hotelDetail/hotelDetail",
            })
                .then((res) => {
                    Toast.clear();
                    if (res.status_code == 200) {
                        const base64Image = `data:image/png;base64,${res.data}`;
                        this.qrcode = base64Image;
                        this.showQrcode = true;
                        this.showShare = false;
                    } else {
                        Toast(res.message);
                    }
                })
                .catch(() => {
                    Toast.clear();
                });
        },

        startPress() {
            // 设置一个定时器，在用户按下按钮后1秒触发长按事件
            this.pressTimer = setTimeout(() => {
                this.onLongPress();
            }, 1000);
        },

        endPress() {
            // 清除定时器，如果用户释放了按钮或移出了按钮区域
            clearTimeout(this.pressTimer);
        },

        onLongPress() {
            const { id, hotel_id } = this.shareItem;

            // 长按事件处理函数
            share({
                lnr_id: id,
                hotel_id: hotel_id,
                share_type: 2,
            })
                .then(() => {})
                .catch(() => {});
        },

        shareLnr() {
            const _this = this;
            let _hotels = JSON.parse(localStorage.getItem("hotels"));
            let page = "";
            if (this.type == 4) {
                page = `pages/home/home?corporateId=${_this.shareItem.agreement_price_code}`;
            } else {
                page = `bookingFlow/hotelDetail/hotelDetail?hotelCode=${_hotels.hotel_code}&commercialCorporateId=${_this.shareItem.agreement_price_code}&utm_source=${_this.shareItem.user.merlinid}`;
            }
            wx.invoke(
                "launchMiniprogram",
                {
                    appid: "wx6c93945039c16dec", // 需跳转的小程序appid
                    path: page, // 所需跳转的小程序内页面路径及参数。非必填
                },
                (res) => {
                    _this.showShare = false;
                    if (res.err_msg == "launchMiniprogram:ok") {
                        share({
                            lnr_id: _this.shareItem.id,
                            hotel_id: _this.shareItem.hotel_id,
                            share_type: 1,
                        })
                            .then(() => {})
                            .catch(() => {});
                    }
                }
            );
        },
        shareActivity() {
            const _this = this;
            console.log(_this.shareItem);

            //只有lnr的有活动分享 be没有
            if (_this.type == 4) {
                return;
            }

            let page = `marketing/ctp/campaign?code=mgm&utm_source=WeComLNR&utm_campaign=CTPMGM&utm_medium=CTP&corpId=${_this.shareItem.agreement_price_code}`;

            wx.invoke(
                "launchMiniprogram",
                {
                    appid: "wx6c93945039c16dec", // 需跳转的小程序appid
                    path: page, // 所需跳转的小程序内页面路径及参数。非必填
                },
                (res) => {
                    _this.showShare = false;
                    if (res.err_msg == "launchMiniprogram:ok") {
                        if (_this.type != 4) {
                            share({
                                lnr_id: _this.shareItem.id,
                                hotel_id: _this.hotel_id,
                                share_type: 3,
                            })
                                .then(() => {})
                                .catch(() => {});
                        } else {
                            // be_share({
                            //     be_lnr_id: _this.shareItem.id,
                            //     // hotel_id: _this.hotel_id,
                            //     share_type: 3,
                            // })
                            //     .then(() => {})
                            //     .catch(() => {});
                        }
                    }
                }
            );
        },
        onLoad() {
            this.getLnrData();
        },
        getLnrData() {
            if (this.type !== 4) {
                getMyUserLists({
                    user_id: this.id,
                    page: this.page,
                    key: this.search,
                })
                    .then((res) => {
                        let { current_page, last_page, data } = res.data;
                        // 加载状态结束
                        this.loading = false;
                        // 数据全部加载完成
                        if (current_page == last_page) {
                            this.finished = true;
                        } else {
                            this.finished = false;
                            this.page++;
                        }
                        this.list = [...this.list, ...data];
                    })
                    .catch((err) => {
                        this.loading = false;
                        console.log(err);
                    });
                return;
            }
            beUserLnrLists({
                user_id: this.id,
                page: this.page,
                key: this.search,
            })
                .then((res) => {
                    let { current_page, last_page, data } = res.data;
                    // 加载状态结束
                    this.loading = false;
                    // 数据全部加载完成
                    if (current_page == last_page) {
                        this.finished = true;
                    } else {
                        this.finished = false;
                        this.page++;
                    }
                    this.list = [...this.list, ...data];
                })
                .catch((err) => {
                    this.loading = false;
                    console.log(err);
                });
        },
        goPage(page, id) {
            this.$router.push({
                path: page,
                query: {
                    id,
                },
            });
        },
        changeLanguage() {
            if (this.$i18n.locale == "zh") {
                this.$i18n.locale = "en";
                this.$cookies.set("locale", "en");
            } else {
                this.$i18n.locale = "zh";
                this.$cookies.set("locale", "zh");
            }
        },
    },
};
</script>
<style type="text/css" lang="scss" scoped>
.page {
    background-color: #fff;
}

.logo {
    background-color: #fff;

    .tab-app {
        display: flex;
        align-items: center;
        font-size: 24px;
        color: #333333;

        img {
            width: 31px;
            height: 30px;
            margin-right: 6px;
        }
    }

    .right-ic {
        color: #333333;
    }
}

.bg {
    width: 100%;
    height: auto;

    img {
        width: 100%;
        height: auto;
    }

    position: relative;

    .user {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 100%;
        padding: 0 30px;
        box-sizing: border-box;
        position: absolute;
        top: 50px;
        left: 0;
        z-index: 10;

        .user-left {
            color: #fff;

            .user-name {
                font-size: 36px;

                .tag {
                    position: relative;
                    top: 4px;
                    margin-left: 12px;
                    display: inline-block;
                    max-width: 300px;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    padding: 8px 12px;
                    border-radius: 18px 18px 18px 0;
                    font-size: 20px;
                    background-color: #e8542c;
                }
            }

            .user-text {
                font-size: 24px;
            }
        }

        .user-img {
            width: 120px;
            height: 120px;
            flex-shrink: 0;
            margin-left: 24px;
            border-radius: 50%;
            border: 1px solid #fff;
            overflow: hidden;

            img {
                width: 100%;
                height: 100%;
            }
        }
    }
}

.content {
    position: relative;
    top: -50px;
    left: 0;
    z-index: 100;
    padding-bottom: 80px;
    background-color: #fff;
    border-radius: 30px 30px 0 0;

    .search-wrap {
        padding-top: 40px;
        padding-bottom: 16px;
        margin: 0 30px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .tab-text {
            font-size: 30px;
            color: #666666;

            .ic {
                width: 14px;
                height: 10px;
                margin-left: 12px;
                position: relative;
                top: -4px;
            }
        }

        .search {
            position: relative;
            z-index: 10;
            padding: 0 28px;
            width: 68%;
            height: 74px;
            // margin-left: 24px;
            border: 2px solid #1f4456;
            background-color: #fff;
            box-sizing: border-box;
            border-radius: 45px;
            display: flex;
            align-items: center;
            justify-content: space-between;

            input {
                font-size: 24px;
                color: #666;
                width: 100%;
                height: 100%;
            }

            .ic {
                width: 28px;
                height: 28px;
                flex-shrink: 0;
            }
        }
    }

    .card {
        margin: 24px 30px 0;
        padding: 30px;
        box-sizing: border-box;
        border-radius: 10px;
        box-shadow: 0 0 24px #eee;
        position: relative;

        .title {
            font-size: 30px;
            color: #333333;
            padding-bottom: 10px;
            font-weight: bold;

            &.small {
                font-size: 24px;
                margin-top: 8px;
            }
        }

        .tag-wrap {
            display: flex;
            flex-wrap: wrap;

            .tag {
                padding: 8px 18px;
                border-radius: 0 8px 8px 8px;
                color: #e8542c;
                font-size: 22px;
                margin-right: 12px;
                background-color: #fbe8e2;

                &.not {
                    color: #fff;
                    background-color: #f01a1a;
                }

                &.success {
                    color: #fff;
                    background-color: #09bb00;
                }

                &.await {
                    color: #fff;
                    background-color: #e8542c;
                }
            }
        }

        .info {
            margin-top: 40px;
            font-size: 24px;
            color: #666666;

            .num {
                color: #1f4456;
            }
        }

        .btn {
            position: absolute;
            right: 30px;
            bottom: 30px;
            color: #fff;
            font-size: 28px;
            padding: 12px 24px;
            border-radius: 10px;
            background-color: #1f4456;

            &.not {
                background-color: #babfc1;
            }
        }
    }
}

.add-btn {
    position: fixed;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
    z-index: 1000;
    width: 100%;
    height: 114px;
    max-width: 750px !important;
    color: #fff;
    font-size: 28px;
    background-color: #fff;
    box-shadow: 0 2px 24px #eee;
    padding: 14px 30px;

    .btn {
        width: 100%;
        padding: 21px 0;
        border-radius: 10px;
        text-align: center;
        background-color: #e8542c;
    }
}

.van-dialog {
    width: 80%;
    padding: 80px 58px;
    box-sizing: border-box;

    .van-dialog__header {
        font-size: 36px;
        color: #333333;
        font-weight: bold;
        margin-bottom: 30px;
    }

    .van-dialog__content {
        font-size: 22px;
        color: #666666;
        margin-bottom: 34px;

        .van-dialog__message {
            line-height: 34px;
        }
    }

    .van-goods-action-button--danger {
        width: 80%;
        height: 74px;
        font-size: 24px;
        color: #fff;
        background: #1f4456 !important;
        border-radius: 10px;
    }
}

.van-popup-share {
    border-radius: 30px 30px 0 0;
}

.popup-share {
    padding-bottom: 54px;
    background: #f2f3f5;

    .share-list {
        display: flex;
        align-items: center;
        justify-content: space-around;
        padding-top: 30px;

        .share-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-between;
            padding: 0 30px;
            box-sizing: border-box;
            font-family: PingFang SC;
            font-weight: 500;
            font-size: 20px;
            color: #333333;

            img {
                width: 106px;
                height: 106px;
                margin-bottom: 14px;
            }
        }
    }

    .colse-share {
        padding: 68px 0 14px;
        text-align: center;
        font-family: PingFang SC;
        font-weight: bold;
        font-size: 32px;
        color: #333333;
    }
}

.van-popup-qrcode {
    width: 100%;
    background: transparent;

    .qrcode-wrap {
        margin: 0 auto 28px;
        padding-top: 60px;
        width: 600px;
        height: 940px;
        position: relative;

        .qrcode {
            width: 100%;
            height: 100%;
            background: #ffffff;
            border-radius: 30px;
            display: flex;
            justify-content: center;
            align-items: center;

            .qrcode-img {
                width: 450px;
                height: 450px;
            }
        }

        .van-icon {
            position: absolute;
            top: 10px;
            right: -48px;
            font-size: 48px;
            color: #ffffff;
        }
    }

    .qrcode-cue {
        margin: 0 auto;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: 30px;
        font-weight: bold;
        color: #333333;

        & > img {
            width: 65px;
            height: 80px;
        }

        .cue-text {
            display: inline-flex;
            align-items: center;
            font-family: PingFang SC;
            font-weight: bold;
            font-size: 36px;
            color: #ffffff;

            img {
                width: 53px;
                height: 75px;
                margin-right: 14px;
            }
        }
    }

    .popup-share {
        border-radius: 30px 30px 0 0;
    }
}

:deep(.van-toast__text) {
    white-space: nowrap;
}
</style>
